<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll';

  export default {
    name: "BetterScroll",
    props:{
      probeType: {
        type: Number,
        default: 0
      },
      pullUpLoad: {
        type: Boolean,
        default: false
      }
    },
    data(){
      return {
        scroll: null,
        message:1
      };
    },
    methods:{
      /**
       * 跳转到一个需要的地方
       */
      scrollTo(x,y,time=500){
        this.scroll && this.scroll.scrollTo(x,y,time);
      },
      /**
       * better-scroll刷新
       */
      refresh(){
        this.scroll && this.scroll.refresh();
      },
      finishPullUp(){
        this.scroll && this.scroll.finishPullUp();
      },
      betterScrollY(){
        return this.scroll ? this.scroll.y : 0;
      },
      __initScroll(){
        this.scroll = new BScroll(this.$refs.wrapper,{
          click: true,
          probeType: this.probeType,
          pullUpLoad: this.pullUpLoad,
        });
        /**
         * 监听滚动事件
         */
        this.scroll.on('scroll',(position) => {
          this.$emit("betterScroll",position);
        });

        /**
         * 上拉加载
         * 判断是否需要上拉
         **/
        if(this.pullUpLoad){
          this.scroll.on('pullingUp',() => {
            this.$emit('pullingUp');
          });
        }
      }
    },
    mounted() {
      this.__initScroll();
    }
  }
</script>

<style scoped>

</style>
